#include <stdio.h>
#include <stdlib.h>


#include "queue.h"
#include "sqstack.h"

#define NR_BALL 27
static int check(queue *qu)
{
	int i = (qu->head + 1)%MAXSIZE;//请查看queue.c中对队列的规定
	
	while(i != qu->tail)
	{
		if(qu->data[i] > qu->data[(i+1)%MAXSIZE])
			return 0;
		i = (i+1)%MAXSIZE;
	}

	return 1;
}
/*
 * 未完成 段错误
 * */
int main()
{
	queue *qu;
	sqstack *st_min,*st_fivemin,*st_hour;

	qu = qu_create();
	if(qu == NULL)
		exit(1);

	st_min = st_create();
	if(st_min == NULL)
		exit(1);

	st_min = st_create();
	if(st_fivemin == NULL)
		exit(1);

	st_hour = st_create();
	if(st_hour == NULL)
		exit(1);

	int i = 1;
	int ret = 0;
	for (i = 1;i <= NR_BALL;i++)
	{
		ret = qu_enqueue(qu,&i);
		if (ret == -1)
		{
			printf("%d enqueue error.\n",i);
			exit(1);
		}
	}
	qu_travel(qu);


	int time = 0;
	int t = 0,value = 0;
	while(1)
	{
		qu_dequeue(qu,&t);
		time ++;
		printf("time = %d\n",time);

		if (st_min->top != 3)
		{
			st_push(st_min,&t);
		}
		else
		{
			printf("111\n");
			while(!st_isempty(st_min))
			{
				st_pop(st_min,&value);
				qu_enqueue(qu,&value);
			}

			if (st_fivemin ->top != 10)
				st_push(st_fivemin,&t);
			else
			{
				while(!st_isempty(st_fivemin))
				{
					st_pop(st_fivemin,&value);
					qu_enqueue(qu,&value);
				}

				if(st_hour->top != 10)
					st_push(st_hour,&t);
				else
				{
					while(!st_isempty(st_hour))
					{
						st_pop(st_hour,&value);
						qu_enqueue(qu,&value);
					}
					
					qu_enqueue(qu,&t);//3个栈都放满的 最后一个 入队
					qu_travel(qu);

					if(check(qu))
						break;
				}
			}
		}
	}

//	printf("time = %d\n",time);

	qu_destory(qu);

	st_destroy(st_min);
	st_destroy(st_fivemin);
	st_destroy(st_hour);

	exit(0);
}
